<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>轮播图</title>
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
    <style>
        body{margin: 0;padding:0;box-sizing: border-box;}
        #pic{width:1200px;height:700px;position: absolute;top:0;left:0;}
        #pic div{width:1200px;height:700px;position: absolute;top:0;left:0;}
        #btn{width:1200px;height:100px;position: absolute;top:750px;left:20%;}
        span{width:120px;float:left;border:1px solid #ddd;background: #ddd;}
        .bj1{background:url(../imgs/title.png);background-repeat: no-repeat;background-size: 100%;}
        .bj2{background:url(../imgs/back.jpeg);background-repeat: no-repeat;background-size: 100%;}
        .bj3{background:url(../imgs/title.png);background-repeat: no-repeat;background-size: 100%;}
        .bj4{background:url(../imgs/back.jpeg);background-repeat: no-repeat;background-size: 100%;}
        .active{background-color: #ff0;}
    </style>
</head>
<body>
<div id="box">
    <div id="pic">
        <div class="bj1"></div>
        <div class="bj2"></div>
        <div class="bj3"></div>
        <div class="bj4"></div>
    </div>
    <div id="btn">
        <span class="btn1 btn active">一按钮</span>
        <span class="btn2 btn">二按钮</span>
        <span class="btn3 btn">三按钮</span>
        <span class="btn4 btn">四按钮</span>
    </div>
</div>
</body>
</html>
<script>
    $(function(){
        var timer ;
        var $index = 0;
        function autoPlay($index){
            timer = setInterval(function(){
                if($index >=($('#pic div').length - 1)){
                    $index = 0;
                }else{
                    $index ++;
                }
                console.log($index);
                $('#pic div').eq($index).show().siblings().hide();
                $('#btn span').eq($index).addClass('active').siblings().removeClass('active');
            },5000)
        }
        autoPlay($index);

        $('.btn').on('click',function(){
            clearInterval(timer);
            $index = $(this).index();
            //  为了避免autoPlay()还需要演延迟5秒才会播放,所以一点击就让此显示
            $('#pic div').eq($index).show().siblings().hide();
            $('#btn span').eq($index).addClass('active').siblings().removeClass('active');
            autoPlay($index);
        })
    })

</script>